package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.feign.item.ItemFeignClient;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.SkuImage;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.math.BigDecimal;
import java.util.List;

/**
 * 浏览器 http://item.gmall.com/{skuId}.html ==> 网关 ==> web-all ==> {skuId}.html
 */
@Controller
public class SkuController {

    @Autowired
    ItemFeignClient itemFeignClient;


    /**
     * 商品详情页:需要优化
     * @param skuId
     * @return
     */
    @GetMapping("/{skuId}.html")
    public String skuPage(@PathVariable("skuId")Long skuId, Model model) {

        new Thread(()->{
// TODO 1.sku的基本信息(名字,id,xxx,价格,sku_描述) skuInfo
            SkuInfo skuInfoById = itemFeignClient.getSkuInfoById(skuId);
            System.out.println(skuInfoById);
        }).start();

        //TODO 把页面需要的所有数据准备好(远程调用service-item 拿到所有数据)



        // TODO 2.sku图片信息
        List<SkuImage> skuImages = itemFeignClient.getSkuImages(skuId);
        //设置sku图片信息
        skuInfoById.setSkuImageList(skuImages);

        // TODO 3.sku分类信息
        BaseCategoryView baseCategoryView = itemFeignClient.getSkuCategoryView(skuId);
        model.addAttribute("CategoryView",baseCategoryView);

        // TODO 4.sku销售属性信息
        List<SpuSaleAttr> spuSaleAttrs = itemFeignClient.getSpuSaleAttrs(skuId,skuInfoById.getSpuId());
        model.addAttribute("spuSaleAttrs",spuSaleAttrs);

        // TODO 5.sku价格信息[专门再查一遍]
        BigDecimal price = itemFeignClient.getSpuPrice(skuId);
        model.addAttribute("price",price);

        //跳转到商品详情页

        //给页面放需要的值
        model.addAttribute("skuInfo",skuInfoById);

        return "item/index";
    }
}
